#
# Cookbook Name:: was8.5
# Recipe:: install
#
# Copyright 2013, IBM Corporation
#
# All rights reserved - Do Not Redistribute
#

# Override this from databag
wasuser = node[:was][:user]
wasuserpw = node[:was][:userpw]
cuser = node[:chef][:user]
cgroup = node[:chef][:group]
  
was_tmp_dir = node[:common][:inst_temp]+"/was"

deployment = node[:deployment]
dbhost=""
search(:node, "deployment:#{deployment}").each do |anode|
	if anode.role?("om_runtdb2")
		dbhost = anode[:fqdn]
	end
end
jmshost=""
search(:node, "deployment:#{deployment}").each do |anode|
    if anode.role?("om_mq")
        jmshost = anode[:fqdn]
    end
end

include_recipe "was::createusergroup"
include_recipe "installation_manager::install"

# Update the silent install template file
template "tmp/#{node[:was][:silent_install_file]}" do
  source "#{node[:was][:silent_install_file]}.erb"
  owner cuser
  group cgroup
  variables(:java_base_repo => "#{was_tmp_dir}/#{node[:was][:java7_tmp_dir]}/#{node[:was][:base_repo]}",
  	:wasnd_base_repo => "#{was_tmp_dir}/#{node[:was][:wasnd_tmp_dir]}/#{node[:was][:base_repo]}",
  	:wassuppl_base_repo => "#{was_tmp_dir}/#{node[:was][:wasnd_suppl_tmp_dir]}/#{node[:was][:base_repo]}",
  	:java_fp_repo => "#{was_tmp_dir}/#{node[:was][:java7_tmp_dir]}/#{node[:was][:fp_repo]}",
  	:wasnd_fp_repo => "#{was_tmp_dir}/#{node[:was][:wasnd_tmp_dir]}/#{node[:was][:fp_repo]}",
  	:wassuppl_fp_repo => "#{was_tmp_dir}/#{node[:was][:wasnd_suppl_tmp_dir]}/#{node[:was][:fp_repo]}",
  	:was855_repo => "#{was_tmp_dir}/#{node[:was][:was855_tmp_dir]}",
  	:ihs_offering_id => node[:was][:ihs_offering_id],
  	:ihs_version => node[:was][:ihs_version],
  	:ihs_profile => node[:was][:ihs_profile],
  	:ihs_features => node[:was][:ihs_features],
  	:ihs_install_loc => node[:was][:ihs_install_loc],
  	:ihs_http_port => node[:was][:ihs_http_port],
  	:was_offering_id => node[:was][:was_offering_id],
  	:was_version => node[:was][:was_version],
  	:was_profile => node[:was][:was_profile],
  	:was_install_loc => node[:was][:was_install_loc],
  	:was_features => node[:was][:was_features],
  	:java_offering_id => node[:was][:java_offering_id],
  	:java_version => node[:was][:java_version],
  	:java_profile => node[:was][:java_profile],
  	:java_features => node[:was][:java_features],
  	:plg_offering_id => node[:was][:plg_offering_id],
  	:plg_version => node[:was][:plg_version],
  	:plg_profile => node[:was][:plg_profile],
  	:plg_features => node[:was][:plg_features],
  	:plg_install_loc => node[:was][:plg_install_loc],
  	:im_shared_loc => node[:im][:im_shared_loc],
  	:install_ihs => node[:was][:install_ihs],
  	:install_was_plugins => node[:was][:install_was_plugins])
  action :create
end

#creating directory that will hold the migration shell script
directory "/migration" do
  user cuser
  group cgroup
  action :create
end

#create the migration shell script from template and put the script in /migration folder
template "/migration/appserver.sh" do
  source "appserver.sh.erb"
  backup false
  owner cuser
  group cgroup
  mode 0755
  variables(
  			:jmshost=>jmshost,
  			:dbhost=>dbhost
  )
  action :create
end

include_recipe "was::prepareinstall"

# Install the WAS 
bash "install_was_server" do
  user "root"
  creates "/tmp/status16"
  cwd "#{node[:im][:installDir]}/eclipse"
  code <<-EOH
  set -e
  ./IBMIM -input /tmp/#{node[:was][:silent_install_file]} -log #{node[:was][:was_install_log_file]} -silent -nosplash -acceptLicense
  touch "/tmp/status16"
  EOH
end

#bash "change ownership of WAS install dir" do
  #user "root"
  #code <<-EOH
  	#chown -R #{node[:was][:user]}:#{node[:was][:group]} #{node[:was][:was_install_loc]}
  	#chown -R #{node[:was][:user]}:#{node[:was][:group]} #{node[:was][:plg_install_loc]}
  	#chown -R #{node[:was][:user]}:#{node[:was][:group]} #{node[:was][:ihs_install_loc]}
  #EOH
#end

# create a script to clean up this VM
template "/tmp/clean-installers.sh" do
  source "clean-installers.sh.erb"
  backup false
  owner "root"
  group "root"
  mode 0755
  variables(
        :tmpdir=>"#{was_tmp_dir} #{node[:im][:im_tmp_dir]}"
  )
  action :create
end

